
package com.core.api;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.core.widget.pulltorefresh.PullToRefreshBase.Mode;
import com.core.widget.pulltorefresh.PullToRefreshListView;

public abstract class B5MBasePaggingResponse<T> extends B5MBaseResponse
{
    private List<T> totallist, currentList;
    private boolean firstPage;//是否第一页
    private boolean lastPage;//是否最后一页
    private int number;//当前第几页
    private int numberOfElements;//当前页元素个数
    private int size;//每页大小
    private int totalElements;//总共多少元素
    private int totalPages;//总共多少页
    private PullToRefreshListView pullToRefreshListView;

    public List<T> getTotallist()
    {
        return totallist;
    }

    public void setTotallist(List<T> totallist)
    {
        this.totallist = totallist;
    }

    public List<T> getCurrentList()
    {
        return currentList;
    }

    public void setCurrentList(List<T> currentList)
    {
        this.currentList = currentList;
    }

    public boolean isFirstPage()
    {
        return firstPage;
    }

    public void setFirstPage(boolean firstPage)
    {
        this.firstPage = firstPage;
    }

    public boolean isLastPage()
    {
        return lastPage;
    }

    public void setLastPage(boolean lastPage)
    {
        this.lastPage = lastPage;
    }

    public int getNumber()
    {
        return number;
    }

    public void setNumber(int number)
    {
        this.number = number;
    }

    public int getNumberOfElements()
    {
        return numberOfElements;
    }

    public void setNumberOfElements(int numberOfElements)
    {
        this.numberOfElements = numberOfElements;
    }

    public int getSize()
    {
        return size;
    }

    public void setSize(int size)
    {
        this.size = size;
    }

    public int getTotalElements()
    {
        return totalElements;
    }

    public void setTotalElements(int totalElements)
    {
        this.totalElements = totalElements;
    }

    public int getTotalPages()
    {
        return totalPages;
    }

    public void setTotalPages(int totalPages)
    {
        this.totalPages = totalPages;
    }

    public PullToRefreshListView getPullToRefreshListView()
    {
        return pullToRefreshListView;
    }

    public void setPullToRefreshListView(PullToRefreshListView pullToRefreshListView)
    {
        this.pullToRefreshListView = pullToRefreshListView;
    }

    @Override
    public void parseJsonArray(JSONArray jsonArray)
    {
        // TODO Auto-generated method stub
        initList();
        currentList.clear();
        if (jsonArray == null)
        {
            return;
        }
        int length = jsonArray.length();
        for (int i = 0; i < length; i++)
        {
            JSONObject jsonObject = jsonArray.optJSONObject(i);
            T t = parseArrayItem(jsonObject);
            if (t != null)
            {
                currentList.add(t);
            }
        }
        if (!currentList.isEmpty())
        {
            totallist.addAll(currentList);
        }
    }

    private void initList()
    {
        if (totallist == null)
        {
            totallist = new ArrayList<T>();
        }
        if (currentList == null)
        {
            currentList = new ArrayList<T>();
        }
    }

    protected abstract T parseArrayItem(JSONObject jsonObject);

    @Override
    public void parseJsonObject(JSONObject jsonObject)
    {
        // TODO Auto-generated method stub
        setFirstPage(jsonObject.optBoolean("firstPage"));
        setLastPage(jsonObject.optBoolean("lastPage"));
        setNumber(jsonObject.optInt("number"));
        setNumberOfElements(jsonObject.optInt("numberOfElements"));
        setSize(jsonObject.optInt("size"));
        setTotalElements(jsonObject.optInt("totalElements"));
        setTotalPages(jsonObject.optInt("totalPages"));
        parseJsonArray(jsonObject.optJSONArray(getResultKey()));
        if (pullToRefreshListView != null)
        {
            pullToRefreshListView.setMode(lastPage == true ? Mode.PULL_FROM_START : Mode.BOTH);
        }
    }

    public boolean isReachTheEnd()
    {
        return lastPage;
    }

    public String getResultKey()
    {
        // TODO Auto-generated method stub
        return "content";
    }
}
